package org.hackystat.devcathlon.ui.page.scoreboard.commentpanel;

import static org.junit.Assert.assertNull;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.TestPanelSource;
import org.apache.wicket.util.tester.WicketTester;
import org.hackystat.devcathlon.configuration.ConfigurationManager;
import org.hackystat.devcathlon.engine.event.Event;
import org.hackystat.devcathlon.engine.event.impl.NoMias;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.test.DevcathlonTestHelper;
import org.hackystat.devcathlon.ui.DevcathlonApplication;
import org.hackystat.devcathlon.ui.page.scoreboard.ScoreboardPage;
import org.hackystat.sensorbase.resource.projects.jaxb.Project;
import org.junit.Before;
import org.junit.Test;

/**
 * Testing the front comment panel.
 * 
 * @author Daniel Arakaki
 */
public class TestFrontCommentPanel extends DevcathlonTestHelper {

  private int oneNoDevTime = -10;
  private int allNoDevTime = -20;
  private int timePeriod = 3;
  private List<Match> matchList;
  //private Comment comment;
  private DevcathlonApplication devcathlonApplication;
  
  /**
   * Instantiate objects and configure properties for testing.
   * 
   * @throws Exception If error during configuration and instantiation.
   */
  @Before
  public void setUp() throws Exception {
    String eventName = NoMias.eventName;
    ConfigurationManager configManager = ConfigurationManager.getInstance();
    configManager.setBaseScoreWeight(eventName, 1);
    configManager.getConfiguration(eventName);
    configManager.setWakeupInterval(eventName, 1440);
    configManager.addProperty(eventName, NoMias.oneNoDevTimeKey, String.valueOf(oneNoDevTime));
    configManager.addProperty(eventName, NoMias.allNoDevTimeKey, String.valueOf(allNoDevTime));
    configManager.addProperty(eventName, NoMias.timePeriodKey, String.valueOf(timePeriod));
    
    String sensorBaseHost = "http://localhost:9976/sensorbase/";
    String dailyProjectDataHost = "http://localhost:9977/dailyprojectdata/";
    
    String ownerOne = "TheOwner";
    String ownerTwo = "owner2";
    //User user = UserManager.getInstance().getUser("TheOwner@hackystat.org");
    Team testTeam = this.makeTeam("TestTeam", ownerOne, "Member1");
    Team testTeamTwo = this.makeTeam("TestTeam2", ownerTwo, "Member2");
    
    Project testProject = this.getProject(ownerOne, "TestTeam");
    Project testProjectTwo = this.getProject(ownerTwo, "TestTeam2");
    
    Event event = new NoMias();
    //this.comment = new Comment(user, Tstamp.makeTimestamp(), "Hello Java!");
    
    Match testMatch = 
      this.addTwoTeamMatch("TestMatch", testTeam, testTeamTwo, testProject, testProjectTwo, event);
    
    this.matchList = new ArrayList<Match>();
    this.matchList.add(testMatch);
    this.devcathlonApplication = new DevcathlonApplication(dailyProjectDataHost, sensorBaseHost);
  }
  
  /**
   * Test the assurance of the comment panel.
   * 
   * @throws Exception When an error occurs.
   */
  @Test
  public void testCommentPanel() throws Exception {
    WicketTester wicketTester = new WicketTester(devcathlonApplication);
    wicketTester.startPage(devcathlonApplication.getHomePage());
    wicketTester.assertRenderedPage(devcathlonApplication.getHomePage());
    
    FormTester formTester = wicketTester.newFormTester("loginForm");
    formTester.setValue("user", "admin@hackystat.org");
    formTester.setValue("password", "admin@hackystat.org");
    formTester.submit();
    
    wicketTester.clickLink("ScoreboardLink");
    wicketTester.assertRenderedPage(ScoreboardPage.class);
    //wicketTester.assertRenderedPage(GameInfoPage.class);
    wicketTester.startPanel(new TestPanelSource() {
      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      public Panel getTestPanel(String wicketId) {
        return new FrontCommentPanel(wicketId, matchList);
      }
    });
    
    wicketTester.assertLabel("panel:matchNameHeader", "TestMatch");
    assertNull("comments do not exist", wicketTester.getTagByWicketId("commentList"));
    //wicketTester.assertLabel("panel:comments", comment.getComment());
    
  }
}
